//
// Created by 覃州 on 2024/1/26.
//

#include "ioc_container.h"
#include <iostream>

using namespace Canon::Component;
using namespace std;

struct Base
{
    virtual void Func() {}
    virtual ~Base() {}
};

struct DevrivedB : public Base
{
    DevrivedB(int a, double b) : m_a(a), m_b(b) {}
    void Func() override { std::cout << m_a + m_b << std::endl; }

private:
    int m_a;
    double m_b;
};

struct DevrivedC : public Base
{
    DevrivedC() { std::cout << "DevrivedC" << std::endl; }
};

struct A
{
    A(Base *ptr) : m_ptr(ptr) {}
    ~A()
    {
        if (m_ptr) {
            delete m_ptr;
            m_ptr = nullptr;
        }
    }

    void Func() { m_ptr->Func(); }

private:
    Base *m_ptr;
};

struct DevrivedD
{
    DevrivedD() = default;
    void Func() {}
};

void TestFunc()
{
    IocContainer ioc;
    ioc.RegisterType<A, DevrivedC>("C");
    auto c = ioc.ResolveShared<A>("C");

    ioc.RegisterType<A, DevrivedB, int, double>("B");
    auto b = ioc.ResolveShared<A>("B", 1, 2.4);
    b->Func();

    //     ioc.RegisterType<A, DevrivedD>("d");
}

int main()
{
    TestFunc();
    return 0;
}